/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_HAL_KEY_H__
#define __PONY_HAL_KEY_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
#include <stddef.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#define KEY_

/* typedefs ------------------------------------------------------------------*/

typedef int (*key_event_cb)(uint8_t id, uint8_t event);

typedef enum hal_key_event {
    HAL_KEY_EVENT_NONE = 0,
    HAL_KEY_EVENT_DOWN,
    HAL_KEY_EVENT_UP,
    HAL_KEY_EVENT_LONG_PRESS,
    HAL_KEY_EVENT_CLICK,
    HAL_KEY_EVENT_DOUBLE_CLICK,
    HAL_KEY_EVENT_TRIPLE_CLICK,
    HAL_KEY_EVENT_NUM,
} hal_key_event_t;

typedef enum hal_key_state {
    /* standard state */
    HAL_KEY_STATE_RELEASED,
    HAL_KEY_STATE_PRESSED,
    /* simulated state */
    HAL_KEY_STATE_IDLE, /* default */
    HAL_KEY_STATE_SHORT_PRESSED,
    HAL_KEY_STATE_LONG_PRESSED,
} hal_key_state_t;

typedef struct hal_key {
    struct hal_key *next;
    uint8_t id;        /* key id */
    uint8_t state;     /* key state (auto control) */
    uint8_t click_cnt; /* click count (auto control) */
    uint8_t tick_cnt;  /* scan tick count (auto control) */
    uint8_t (*get_state)(uint8_t id);
} hal_key_t;

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern void hal_key_set_event_cb(key_event_cb cb);
extern void hal_key_attach(hal_key_t *key);
extern void hal_key_scan(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_HAL_KEY_H__ */
